﻿using System.Collections.Generic;
using System.Windows.Input;
using Ninject.Core.Parameters;
using TestBat.IModels.Domain;
using TestBat.IModels.Models;
using TestBat.IViewModels.Workspaces.Gradings;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Base.Commands;
using TestBat.ViewModels.Attributes;

namespace TestBat.ViewModels.Workspaces.Gradings
{
    public class GradingsViewModel : WorkspaceViewModel, IGradingsViewModel
    {
        public GradingsViewModel(IScheduleModel scheduleModel, ITestTaskModel testTaskModel)
        {
            this.scheduleModel = scheduleModel;
            this.testTaskModel = testTaskModel;
            this.showNotGradedOnly = true;

            this.scheduleComboBox = DefaultServiceLocator.Get<IScheduleComboBoxViewModel>();
            this.scheduleComboBox.SelectionChanged += (() =>
                {
                    traineeComboBox.Reset();
                    NotifyPropertyChanged(() => Gradings); // recreate gradings
                });

            this.traineeComboBox = DefaultServiceLocator.Get<ITraineeComboBoxViewModel>();
            this.traineeComboBox.SelectionChanged += (() =>
                {
                    NotifyPropertyChanged(() => Gradings); // recreate gradings
                });
            this.traineeComboBox.SelectedSchedule += (() =>
                {
                    return scheduleComboBox.SelectedItem.Item;
                });
        }

        public bool ShowNotGradedOnly
        {
            get
            {
                return showNotGradedOnly;
            }
            set
            {
                if (value != showNotGradedOnly)
                {
                    showNotGradedOnly = value;

                    NotifyPropertyChanged(() => ShowNotGradedOnly);
                    NotifyPropertyChanged(() => Gradings); // recreate gradings
                }
            }
        }

        public IScheduleComboBoxViewModel ScheduleComboBox
        {
            get
            {
                return scheduleComboBox;
            }
        }

        public ITraineeComboBoxViewModel TraineeComboBox
        {
            get
            {
                return traineeComboBox;
            }
        }

        public IList<IGradingViewModel> Gradings
        {
            get
            {
                IList<IGradingViewModel> gradings = new List<IGradingViewModel>();

                // create gradings based on selection in comboboxes
                IList<ITestTask> testTasks = new List<ITestTask>();
                if (ScheduleComboBox.SelectedItem.Item != null)
                    testTasks = testTaskModel.GetByReferences(ScheduleComboBox.SelectedItem.Item, TraineeComboBox.SelectedItem.Item, ShowNotGradedOnly);
                foreach (ITestTask tt in testTasks)
                {
                    var vm = DefaultServiceLocator.Get<IGradingViewModel>(With.Parameters.ConstructorArgument("testTask", tt));
                    gradings.Add(vm);
                }

                return gradings;
            }
        }

        public ICommand SaveAllCommand
        {
            get
            {
                return new RelayCommand(c =>
                    {
                        SaveAll();

                        NotifyPropertyChanged(() => Gradings); // recreate gradings
                    });
            }
        }

        [BusinessErrorDialog]
        private void SaveAll()
        {
            testTaskModel.SaveAll();
        }

        private readonly IScheduleModel scheduleModel;
        private readonly ITestTaskModel testTaskModel;

        private readonly IScheduleComboBoxViewModel scheduleComboBox;
        private readonly ITraineeComboBoxViewModel traineeComboBox;

        private bool showNotGradedOnly;
    }
}
